package com.gfxy.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gfxy.modules.base.entity.TeacherBaseInfo;
import com.gfxy.modules.base.mapper.TeacherBaseInfoMapper;
import com.gfxy.modules.base.service.ITeacherBaseInfoService;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.system.entity.*;
import com.gfxy.modules.system.mapper.SysUserMapper;
import com.gfxy.modules.system.service.ISysUserDepartService;
import com.gfxy.modules.system.service.ISysUserDepartTeachingService;
import com.gfxy.modules.system.service.ISysUserService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TeacherBaseInfoServiceImpl extends ServiceImpl<TeacherBaseInfoMapper, TeacherBaseInfo>  implements ITeacherBaseInfoService {

    private static final String DEFAULT_PWD = "Gfxy123456";
    private static final String TEACHER_ROLE = "1402832775044915202";

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserDepartTeachingService sysUserDepartTeachingService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @Autowired
    private SysUserMapper userMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TeacherBaseInfoVo teacherBaseInfoVo) {
        SysUser user =this.transformSysUser(teacherBaseInfoVo);

        user.setCreateTime(new Date());
        String salt = oConvertUtils.randomGen(8);
        user.setSalt(salt);
        String passwordEncode = PasswordUtil.encrypt(user.getUsername(), DEFAULT_PWD, salt);
        user.setPassword(passwordEncode);
        user.setStatus(1);
        user.setDelFlag(CommonConstant.DEL_FLAG_0);

        String orgId = teacherBaseInfoVo.getOrgCode();
        sysUserService.saveUser(user, TEACHER_ROLE, orgId);

        if(oConvertUtils.isNotEmpty(teacherBaseInfoVo.getTeachingOrgCode())) {
            SysUserDepartTeaching sysUserDepartTeaching = new SysUserDepartTeaching(teacherBaseInfoVo.getId(),teacherBaseInfoVo.getTeachingOrgCode());
            sysUserDepartTeachingService.save(sysUserDepartTeaching);
        }

        TeacherBaseInfo teacherBaseInfo = this.transformTeacherBaseInfo(teacherBaseInfoVo);
        this.save(teacherBaseInfo);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true)
    public void edit(TeacherBaseInfoVo teacherBaseInfoVo) {

        if(oConvertUtils.isNotEmpty(teacherBaseInfoVo.getOrgCode())) {
            sysUserDepartService.remove(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, teacherBaseInfoVo.getId()));
            SysUserDepart sysUserDepart = new SysUserDepart(teacherBaseInfoVo.getId(), teacherBaseInfoVo.getOrgCode());
            sysUserDepartService.save(sysUserDepart);
        }


        if(oConvertUtils.isNotEmpty(teacherBaseInfoVo.getTeachingOrgCode())) {
            sysUserDepartTeachingService.remove(new QueryWrapper<SysUserDepartTeaching>().lambda().eq(SysUserDepartTeaching::getUserId, teacherBaseInfoVo.getId()));
            SysUserDepartTeaching sysUserDepartTeaching = new SysUserDepartTeaching(teacherBaseInfoVo.getId(),teacherBaseInfoVo.getTeachingOrgCode());
            sysUserDepartTeachingService.save(sysUserDepartTeaching);
        }

        TeacherBaseInfo teacherBaseInfo = this.transformTeacherBaseInfo(teacherBaseInfoVo);

        this.updateById(teacherBaseInfo);
    }

    @Override
    public void resetPassword(String teacherId) {
        SysUser user = userMapper.getUserByName(teacherId);

        String password = PasswordUtil.encrypt(teacherId, DEFAULT_PWD, user.getSalt());

        this.userMapper.update(new SysUser().setPassword(password), new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, user.getId()));

    }


    private SysUser transformSysUser(TeacherBaseInfoVo teacherBaseInfoVo){
        SysUser user = new SysUser();
        user.setId(teacherBaseInfoVo.getId());
        user.setWorkNo(teacherBaseInfoVo.getId());
        user.setRealname(teacherBaseInfoVo.getRealname());
        user.setUsername(teacherBaseInfoVo.getId());
        return user;
    }

    private TeacherBaseInfo transformTeacherBaseInfo(TeacherBaseInfoVo teacherBaseInfoVo){
        TeacherBaseInfo result = new TeacherBaseInfo();
        result.setId(teacherBaseInfoVo.getId());
        result.setEmail(teacherBaseInfoVo.getEmail());
        result.setBirthday(teacherBaseInfoVo.getBirthday());
        result.setPhone(teacherBaseInfoVo.getPhone());
        result.setTeacherType(teacherBaseInfoVo.getTeacherType());
        result.setSex(teacherBaseInfoVo.getSex());
        result.setPosition(teacherBaseInfoVo.getPosition());
        result.setRealname(teacherBaseInfoVo.getRealname());
        result.setTitleRank(teacherBaseInfoVo.getTitleRank());
        result.setStatus(teacherBaseInfoVo.getStatus());
        result.setWork_date(teacherBaseInfoVo.getWork_date());
        result.setDelFlag(teacherBaseInfoVo.getDelFlag());
        return result;
    }
}
